package com.surface.hao.frame;

import com.surface.hao.component.ScrollPanel;
import com.surface.hao.util.ComponentUtils;
import com.surface.hao.util.FileUtils;
import lombok.extern.log4j.Log4j;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

/**
 * @Description
 *  文件查找的面板信息
 * @Date 2019/12/29
 * @Author songhao
 **/
@Log4j
public class FileSearchPanel {


    static String inputDir = "";
    static ScrollPanel tipArea = null;
    static boolean isBlurry = false;//是否模糊匹配
    static boolean isOnlyFile = false;//是否只匹配文件

    public static JPanel getJpanel(String titleName,JFrame jFrame) {
        JPanel panel = new JPanel();
        //设置布局方式和大小
        panel.setLayout(new BorderLayout(0, 0));
        panel.setSize(800, 600);

        JLabel titleLabel = new JLabel(titleName,JLabel.CENTER);
        titleLabel.setBorder(new EmptyBorder(10, 10, 25, 10));
        titleLabel.setFont(new Font("宋体", Font.BOLD, 24));

        //第一个Panel
        JPanel titlePanel = new JPanel();
        titlePanel.setLayout(new BorderLayout(0, 0));
        panel.add(titlePanel, BorderLayout.NORTH);

        //添加标题信息
        titlePanel.add(titleLabel, BorderLayout.NORTH);

        // 时间戳转换为日期这个Panel
        JPanel dirPanel = getDirPanel(jFrame);
        titlePanel.add(dirPanel, BorderLayout.CENTER);

        // 日期字符串转换为时间戳Panel
        JPanel fnamePanel = getFnamePanel();
        titlePanel.add(fnamePanel, BorderLayout.SOUTH);

        //第二个Panel
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BorderLayout(0, 0));
        panel.add(centerPanel, BorderLayout.CENTER);

        tipArea = new ScrollPanel();
        tipArea.setPreferredSize(new Dimension(200, 350));
        centerPanel.add(tipArea, BorderLayout.CENTER);
        return panel;
    }

    private static JPanel getDirPanel(JFrame frame) {
        JPanel panel_2 = new JPanel();
        FlowLayout flowLayout = (FlowLayout) panel_2.getLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);
        panel_2.setBorder(new TitledBorder(UIManager
                .getBorder("TitledBorder.border"), "查找目录",
                TitledBorder.LEADING, TitledBorder.TOP, null,
                new Color(0, 0, 0)));

        JTextField dir1 = new JTextField();
        dir1.setEditable(false);
        dir1.setEnabled(false);
        dir1.setText("");
        dir1.setFont(new Font("宋体", Font.BOLD, 24));
        dir1.setPreferredSize(new Dimension(600, 40));
        panel_2.add(dir1);

        JButton button_2 = new JButton("选择....");
        button_2.setPreferredSize(new Dimension(100, 40));
        button_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                // 获取源目录
                JFileChooser chooser1 = new JFileChooser();
                //设置可以选择文件或者目录
                chooser1.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                chooser1.setToolTipText("请选择源目录");
                int rt = chooser1.showOpenDialog(frame);
                if (rt == JFileChooser.APPROVE_OPTION) {
                    dir1.setText(chooser1.getSelectedFile().getPath());
                    log.info("源目录：" + chooser1.getSelectedFile().getPath()
                            + "请选择目标目录！");
                    inputDir = chooser1.getSelectedFile().getPath();
                    dir1.setForeground(new Color(0, 0, 0));
                }
            }
        });
        panel_2.add(button_2);

        return panel_2;
    }

    /**
     * 获取目录的Panel
     * @return
     */
    public static JPanel getFnamePanel(){
        JPanel panel_1 = new JPanel();
        FlowLayout flowLayout_1 = (FlowLayout) panel_1.getLayout();
        flowLayout_1.setAlignment(FlowLayout.LEFT);
        panel_1.setBorder(new TitledBorder(UIManager
                .getBorder("TitledBorder.border"), "文件名",
                TitledBorder.LEADING, TitledBorder.TOP, null,
                new Color(0, 0, 0)));

        JTextField dir2 = new JTextField();
        dir2.setText("");
        dir2.setFont(new Font("宋体", Font.BOLD, 24));
        dir2.setPreferredSize(new Dimension(600, 40));
        panel_1.add(dir2);

        JButton button_1 = new JButton("搜索");
        button_1.setPreferredSize(new Dimension(100, 40));
        panel_1.add(button_1);

        JCheckBox blurryQuery = new JCheckBox("模糊匹配");
        blurryQuery.setToolTipText("是否需要模糊匹配？");
        blurryQuery.setSelected(true);
        panel_1.add(blurryQuery);

        JCheckBox fileQuery = new JCheckBox("只匹配文件");
        fileQuery.setToolTipText("是否只匹配文件？");
        fileQuery.setSelected(true);
        panel_1.add(fileQuery);

        //设置button监控
        button_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String searchWord = dir2.getText();
                log.info("获取文件路径:[" + inputDir + "]," + searchWord);

                isBlurry = false;
                isOnlyFile = false;
                if(blurryQuery.getSelectedObjects() != null && blurryQuery.getSelectedObjects().length > 0){
                    isBlurry = true;
                    log.info("是否开启模糊搜索----" + isBlurry);
                }

                if(fileQuery.getSelectedObjects() != null && fileQuery.getSelectedObjects().length > 0){
                    isOnlyFile = true;
                    log.info("是否只搜索文件 ----" + isOnlyFile);
                }
                // 新启动一个线程进行检索
                new Thread(() -> searchWord(inputDir, searchWord, tipArea,isBlurry,isOnlyFile)).start();
            }
        });
        return panel_1;
    }

    /**
     * 查询对应的文件内容
     *
     * @param inputDir
     * @param word
     */
    public static void searchWord(String inputDir, String word, ScrollPanel jta, boolean isBlurry, boolean isOnlyFile) {
        if(word == null || "".equals(word.trim())){
            ComponentUtils.setTips(jta,"搜索关键词不可以为空!");
            return;
        }

        java.util.List<String> allChildPaths = new ArrayList<String>();
        FileUtils.getAllChildPaths(inputDir, allChildPaths,isOnlyFile);

        for (String childPath : allChildPaths) {
            log.info("正在查找文件:[" + childPath + "]");
            File file = new File(childPath);
            //模糊匹配
            if(isBlurry){
                if(file.getName().toLowerCase().contains(word.toLowerCase())){
                    ComponentUtils.setTips(jta,"文件路径:[" + childPath + "]");
                }
            }else{
                if(file.getName().equalsIgnoreCase(word)){
                    ComponentUtils.setTips(jta,"文件路径:[" + childPath + "]");
                }
            }
        }
    }
}
